<?php

/**
 * Created by PhpStorm. 合同管理
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:52
 */

namespace app\api\model;

use think\Model;
use think\Db;
use PhpOffice\PhpWord\TemplateProcessor;

class Contractmgr extends Model
{

    // 表名
    protected $name = 'contract';
    /**
     * 插入数据
     * @param $data
     * @return bool|int|string
     */
    public function insertData($data)
    {
        // var_dump(22222);die;
        $r = $this->insertGetId($data);
        if (!$r) {
            return false;
        }

        return $r;
    }

    /**
     * 编辑数据
     * @param $data
     * @param $where
     * @return Project
     */
    public function updateBywhere($data, $where)
    {
        $r = $this->where($where)->update($data);
        return $r;
    }

    /**
     * 获取总数
     * @param array $where
     * @return int|string
     * @throws \think\Exception
     */
    public function countWhere($where = [])
    {
        $r = $this->where($where)->count();
        return $r;
    }

    /**
     * 假删
     * @param array $where
     * @return Project
     */
    public function deleteWhere($where = [])
    {
        $r = $this->where($where)->update(['is_delete' => 1]);
        return $r;
    }

    /**
     * 假删 D表
     * @param array $where
     * @return Project
     */
    public function deleteWhere_d($where = [])
    {
        $r = $this->where($where)->update(['is_delete' => 1]);
        return $r;
    }




    /**
     * 获取详情
     * @param array $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findWhere($where = [])
    {

        $r = $this->where($where)->find();
        return $r;
    }

    /**
     * 分页列表
     * @param array $where
     * @param string $order
     * @param int $current
     * @param int $pagesize
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function listWherePage($where = [], $order = 'id desc', $current = 0, $pagesize = 10)
    {
        $r = $this->where($where)->order($order)->limit($current . ',' . $pagesize)->select();
        return $r;
    }

    public function builddocf1()
    {
        // $phpWord = new \PhpOffice\PhpWord\PhpWord();

        // $templateProcessor = new TemplateProcessor(ROOT_PATH.'static/jianding.docx'); 
        // $templateProcessor-> setValue('keti','测试题目'); 　　 
        // $templateProcessor->saveAs(ROOT_PATH.'static/jianding.docx');

    }

    // 新增明细数据  有且 并更新
    public function insertData_D($data, $tbname)
    {

        //  $data1['account']="222";
        // $r = Db::name('admin')->insertGetId($data1);
        $where = array('mid' => $data['mid'],'sortid'=>$data['sortid']);
        $detail = Db::name($tbname)
            ->where($where)
            ->find();
        if($detail){
            $r = Db::name($tbname)
            ->where($where)
            ->update($data);
        }else{
            $r = Db::name($tbname)->insertGetId($data);
            if (!$r) {
                return false;
            }
        }   
         

       
        return $r;
    }

    // 简单的查询
    public function sel_com_fxlist($data, $where,$tbname)
    {

        //  $data1['account']="222";
        // $r = Db::name('admin')->insertGetId($data1);
        //$where = array('mid' => $data['mid'],'sortid'=>$data['sortid']);
        $detail = Db::name($tbname)
            ->where($where)
            ->find();
        if($detail){
            $r = Db::name($tbname)
            ->where($where)
            ->update($data);
        }else{
            $r = Db::name($tbname)->insertGetId($data);
            if (!$r) {
                return false;
            }
        }   
         

       
        return $r;
    }

    // 


}
